<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>toolbarcontroller.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_editor_toolbarcontroller.js.html">toolbarcontroller.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A class for managing the editor toolbar.
<a name="line17"></a> *
<a name="line18"></a> * @author attila@google.com (Attila Bodis)
<a name="line19"></a> * @author jparent@google.com (Julie Parent)
<a name="line20"></a> * @see ../../demos/editor/editor.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.ui.editor.ToolbarController&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.editor.Field.EventType&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line28"></a>goog.require(&#39;goog.ui.Component.EventType&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * A class for managing the editor toolbar.  Acts as a bridge between
<a name="line34"></a> * a {@link goog.editor.Field} and a {@link goog.ui.Toolbar}.
<a name="line35"></a> *
<a name="line36"></a> * The {@code toolbar} argument must be an instance of {@link goog.ui.Toolbar}
<a name="line37"></a> * or a subclass.  This class doesn&#39;t care how the toolbar was created.  As
<a name="line38"></a> * long as one or more controls hosted  in the toolbar have IDs that match
<a name="line39"></a> * built-in {@link goog.editor.Command}s, they will function as expected.  It is
<a name="line40"></a> * the caller&#39;s responsibility to ensure that the toolbar is already rendered
<a name="line41"></a> * or that it decorates an existing element.
<a name="line42"></a> *
<a name="line43"></a> *
<a name="line44"></a> * @param {!goog.editor.Field} field Editable field to be controlled by the
<a name="line45"></a> *     toolbar.
<a name="line46"></a> * @param {!goog.ui.Toolbar} toolbar Toolbar to control the editable field.
<a name="line47"></a> * @constructor
<a name="line48"></a> * @extends {goog.events.EventTarget}
<a name="line49"></a> */
<a name="line50"></a>goog.ui.editor.ToolbarController = function(field, toolbar) {
<a name="line51"></a>  goog.events.EventTarget.call(this);
<a name="line52"></a>
<a name="line53"></a>  /**
<a name="line54"></a>   * Event handler to listen for field events and user actions.
<a name="line55"></a>   * @type {!goog.events.EventHandler}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.handler_ = new goog.events.EventHandler(this);
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * The field instance controlled by the toolbar.
<a name="line62"></a>   * @type {!goog.editor.Field}
<a name="line63"></a>   * @private
<a name="line64"></a>   */
<a name="line65"></a>  this.field_ = field;
<a name="line66"></a>
<a name="line67"></a>  /**
<a name="line68"></a>   * The toolbar that controls the field.
<a name="line69"></a>   * @type {!goog.ui.Toolbar}
<a name="line70"></a>   * @private
<a name="line71"></a>   */
<a name="line72"></a>  this.toolbar_ = toolbar;
<a name="line73"></a>
<a name="line74"></a>  /**
<a name="line75"></a>   * Editing commands whose state is to be queried when updating the toolbar.
<a name="line76"></a>   * @type {!Array.&lt;string&gt;}
<a name="line77"></a>   * @private
<a name="line78"></a>   */
<a name="line79"></a>  this.queryCommands_ = [];
<a name="line80"></a>
<a name="line81"></a>  // Iterate over all buttons, and find those which correspond to
<a name="line82"></a>  // queryable commands. Add them to the list of commands to query on
<a name="line83"></a>  // each COMMAND_VALUE_CHANGE event.
<a name="line84"></a>  this.toolbar_.forEachChild(function(button) {
<a name="line85"></a>    if (button.queryable) {
<a name="line86"></a>      this.queryCommands_.push(this.getComponentId(button.getId()));
<a name="line87"></a>    }
<a name="line88"></a>  }, this);
<a name="line89"></a>
<a name="line90"></a>  // Make sure the toolbar doesn&#39;t steal keyboard focus.
<a name="line91"></a>  this.toolbar_.setFocusable(false);
<a name="line92"></a>
<a name="line93"></a>  // Hook up handlers that update the toolbar in response to field events,
<a name="line94"></a>  // and to execute editor commands in response to toolbar events.
<a name="line95"></a>  this.handler_.
<a name="line96"></a>      listen(this.field_, goog.editor.Field.EventType.COMMAND_VALUE_CHANGE,
<a name="line97"></a>          this.updateToolbar).
<a name="line98"></a>      listen(this.toolbar_, goog.ui.Component.EventType.ACTION,
<a name="line99"></a>          this.handleAction);
<a name="line100"></a>};
<a name="line101"></a>goog.inherits(goog.ui.editor.ToolbarController, goog.events.EventTarget);
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Returns the Closure component ID of the control that corresponds to the
<a name="line106"></a> * given {@link goog.editor.Command} constant.
<a name="line107"></a> * Subclasses may override this method if they want to use a custom mapping
<a name="line108"></a> * scheme from commands to controls.
<a name="line109"></a> * @param {string} command Editor command.
<a name="line110"></a> * @return {string} Closure component ID of the corresponding toolbar
<a name="line111"></a> *     control, if any.
<a name="line112"></a> * @protected
<a name="line113"></a> */
<a name="line114"></a>goog.ui.editor.ToolbarController.prototype.getComponentId = function(command) {
<a name="line115"></a>  // The default implementation assumes that the component ID is the same as
<a name="line116"></a>  // the command constant.
<a name="line117"></a>  return command;
<a name="line118"></a>};
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Returns the {@link goog.editor.Command} constant
<a name="line123"></a> * that corresponds to the given Closure component ID.  Subclasses may override
<a name="line124"></a> * this method if they want to use a custom mapping scheme from controls to
<a name="line125"></a> * commands.
<a name="line126"></a> * @param {string} id Closure component ID of a toolbar control.
<a name="line127"></a> * @return {string} Editor command or dialog constant corresponding to the
<a name="line128"></a> *     toolbar control, if any.
<a name="line129"></a> * @protected
<a name="line130"></a> */
<a name="line131"></a>goog.ui.editor.ToolbarController.prototype.getCommand = function(id) {
<a name="line132"></a>  // The default implementation assumes that the component ID is the same as
<a name="line133"></a>  // the command constant.
<a name="line134"></a>  return id;
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Returns the event handler object for the editor toolbar.  Useful for classes
<a name="line140"></a> * that extend {@code goog.ui.editor.ToolbarController}.
<a name="line141"></a> * @return {!goog.events.EventHandler} The event handler object.
<a name="line142"></a> * @protected
<a name="line143"></a> */
<a name="line144"></a>goog.ui.editor.ToolbarController.prototype.getHandler = function() {
<a name="line145"></a>  return this.handler_;
<a name="line146"></a>};
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Returns the field instance managed by the toolbar.  Useful for
<a name="line151"></a> * classes that extend {@code goog.ui.editor.ToolbarController}.
<a name="line152"></a> * @return {!goog.editor.Field} The field managed by the toolbar.
<a name="line153"></a> * @protected
<a name="line154"></a> */
<a name="line155"></a>goog.ui.editor.ToolbarController.prototype.getField = function() {
<a name="line156"></a>  return this.field_;
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Returns the toolbar UI component that manages the editor.  Useful for
<a name="line162"></a> * classes that extend {@code goog.ui.editor.ToolbarController}.
<a name="line163"></a> * @return {!goog.ui.Toolbar} The toolbar UI component.
<a name="line164"></a> */
<a name="line165"></a>goog.ui.editor.ToolbarController.prototype.getToolbar = function() {
<a name="line166"></a>  return this.toolbar_;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * @return {boolean} Whether the toolbar is visible.
<a name="line172"></a> */
<a name="line173"></a>goog.ui.editor.ToolbarController.prototype.isVisible = function() {
<a name="line174"></a>  return this.toolbar_.isVisible();
<a name="line175"></a>};
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * Shows or hides the toolbar.
<a name="line180"></a> * @param {boolean} visible Whether to show or hide the toolbar.
<a name="line181"></a> */
<a name="line182"></a>goog.ui.editor.ToolbarController.prototype.setVisible = function(visible) {
<a name="line183"></a>  this.toolbar_.setVisible(visible);
<a name="line184"></a>};
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>/**
<a name="line188"></a> * @return {boolean} Whether the toolbar is enabled.
<a name="line189"></a> */
<a name="line190"></a>goog.ui.editor.ToolbarController.prototype.isEnabled = function() {
<a name="line191"></a>  return this.toolbar_.isEnabled();
<a name="line192"></a>};
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * Enables or disables the toolbar.
<a name="line197"></a> * @param {boolean} enabled Whether to enable or disable the toolbar.
<a name="line198"></a> */
<a name="line199"></a>goog.ui.editor.ToolbarController.prototype.setEnabled = function(enabled) {
<a name="line200"></a>  this.toolbar_.setEnabled(enabled);
<a name="line201"></a>};
<a name="line202"></a>
<a name="line203"></a>
<a name="line204"></a>/**
<a name="line205"></a> * Programmatically blurs the editor toolbar, un-highlighting the currently
<a name="line206"></a> * highlighted item, and closing the currently open menu (if any).
<a name="line207"></a> */
<a name="line208"></a>goog.ui.editor.ToolbarController.prototype.blur = function() {
<a name="line209"></a>  // We can&#39;t just call this.toolbar_.getElement().blur(), because the toolbar
<a name="line210"></a>  // element itself isn&#39;t focusable, so goog.ui.Container#handleBlur isn&#39;t
<a name="line211"></a>  // registered to handle blur events.
<a name="line212"></a>  this.toolbar_.handleBlur(null);
<a name="line213"></a>};
<a name="line214"></a>
<a name="line215"></a>
<a name="line216"></a>/** @override */
<a name="line217"></a>goog.ui.editor.ToolbarController.prototype.disposeInternal = function() {
<a name="line218"></a>  goog.ui.editor.ToolbarController.superClass_.disposeInternal.call(this);
<a name="line219"></a>  if (this.handler_) {
<a name="line220"></a>    this.handler_.dispose();
<a name="line221"></a>    delete this.handler_;
<a name="line222"></a>  }
<a name="line223"></a>  if (this.toolbar_) {
<a name="line224"></a>    this.toolbar_.dispose();
<a name="line225"></a>    delete this.toolbar_;
<a name="line226"></a>  }
<a name="line227"></a>  delete this.field_;
<a name="line228"></a>  delete this.queryCommands_;
<a name="line229"></a>};
<a name="line230"></a>
<a name="line231"></a>
<a name="line232"></a>/**
<a name="line233"></a> * Updates the toolbar in response to editor events.  Specifically, updates
<a name="line234"></a> * button states based on {@code COMMAND_VALUE_CHANGE} events, reflecting the
<a name="line235"></a> * effective formatting of the selection.
<a name="line236"></a> * @param {goog.events.Event} e Editor event to handle.
<a name="line237"></a> * @protected
<a name="line238"></a> */
<a name="line239"></a>goog.ui.editor.ToolbarController.prototype.updateToolbar = function(e) {
<a name="line240"></a>  if (!this.toolbar_.isEnabled() ||
<a name="line241"></a>      !this.dispatchEvent(goog.ui.Component.EventType.CHANGE)) {
<a name="line242"></a>    return;
<a name="line243"></a>  }
<a name="line244"></a>
<a name="line245"></a>  var state;
<a name="line246"></a>
<a name="line247"></a>  /** @preserveTry */
<a name="line248"></a>  try {
<a name="line249"></a>    /** @type {Array.&lt;string&gt;} */
<a name="line250"></a>    e.commands; // Added by dispatchEvent.
<a name="line251"></a>
<a name="line252"></a>    // If the COMMAND_VALUE_CHANGE event specifies which commands changed
<a name="line253"></a>    // state, then we only need to update those ones, otherwise update all
<a name="line254"></a>    // commands.
<a name="line255"></a>    state = /** @type {Object} */ (
<a name="line256"></a>        this.field_.queryCommandValue(e.commands || this.queryCommands_));
<a name="line257"></a>  } catch (ex) {
<a name="line258"></a>    // TODO(attila): Find out when/why this happens.
<a name="line259"></a>    state = {};
<a name="line260"></a>  }
<a name="line261"></a>
<a name="line262"></a>  this.updateToolbarFromState(state);
<a name="line263"></a>};
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>/**
<a name="line267"></a> * Updates the toolbar to reflect a given state.
<a name="line268"></a> * @param {Object} state Object mapping editor commands to values.
<a name="line269"></a> */
<a name="line270"></a>goog.ui.editor.ToolbarController.prototype.updateToolbarFromState =
<a name="line271"></a>    function(state) {
<a name="line272"></a>  for (var command in state) {
<a name="line273"></a>    var button = this.toolbar_.getChild(this.getComponentId(command));
<a name="line274"></a>    if (button) {
<a name="line275"></a>      var value = state[command];
<a name="line276"></a>      if (button.updateFromValue) {
<a name="line277"></a>        button.updateFromValue(value);
<a name="line278"></a>      } else {
<a name="line279"></a>        button.setChecked(!!value);
<a name="line280"></a>      }
<a name="line281"></a>    }
<a name="line282"></a>  }
<a name="line283"></a>};
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>/**
<a name="line287"></a> * Handles {@code ACTION} events dispatched by toolbar buttons in response to
<a name="line288"></a> * user actions by executing the corresponding field command.
<a name="line289"></a> * @param {goog.events.Event} e Action event to handle.
<a name="line290"></a> * @protected
<a name="line291"></a> */
<a name="line292"></a>goog.ui.editor.ToolbarController.prototype.handleAction = function(e) {
<a name="line293"></a>  var command = this.getCommand(e.target.getId());
<a name="line294"></a>  this.field_.execCommand(command, e.target.getValue());
<a name="line295"></a>};
</pre>


</body>
</html>
